import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/loading/loading_dart.dart';
import 'package:flutter_app/model/community_find_model.dart';
import 'package:flutter_app/model/topic_model.dart';
import 'package:flutter_app/screen_page.dart';
import 'package:flutter_app/service/service_method.dart';

import '../../RoutesPath.dart';

class CommunityFindPage extends StatefulWidget {
  @override
  _CommunityFindPageState createState() => _CommunityFindPageState();
}

class _CommunityFindPageState extends State<CommunityFindPage> {
  @override
  void initState() {
    super.initState();
    getTopicListPage("0", "");
  }

  @override
  Widget build(BuildContext context) {
    if (topicList.length != 0) {
      return Container(
        width: double.infinity,
        height: double.infinity,
        child: GridView.builder(
          itemCount: topicList.length,
          itemBuilder: (context, index) => _item(index),
          padding: EdgeInsets.only(left: 16.0, right: 16.0, top: 16.0),
          scrollDirection: Axis.vertical,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 10.0,
              crossAxisSpacing: 20.0,
              childAspectRatio: 0.7),
        ),
      );
    } else {
      return loadWidget();
    }
  }

  Widget _item(int index) {
    return Container(
      width: width((375 - 12 * 3) / 2.toDouble()),
      decoration: BoxDecoration(
          color: Colors.white54,
          borderRadius: BorderRadius.all(Radius.circular(height(6)))),
      child: InkWell(
        onTap: () {
          Navigator.pushNamed(context, RoutesPath.topicDetail,
              arguments: topicList[index].id);
        },
        child: Column(
          children: <Widget>[
            SizedBox(height: height(20)),
            Text("${topicList[index].name}"),
            SizedBox(height: height(20)),
            Text("${topicList[index].totalOpenTimes}条动态")
          ],
        ),
      ),
    );
  }

  List<CommunityFindEntityList> findList = [];
  List<TopicInfoVOs> topicList = [];

  getFirstPage(String nextTalkId, String nextCommentId, int refresh,
      String newInforId, String recommendType, String isPullDown) async {
    var data = {
      "nextTalkId": nextTalkId,
      "nextCommentId": nextCommentId,
      "refresh": refresh,
      "newInforId": newInforId,
      "recommendType": recommendType,
      "isPullDown": isPullDown,
      "showCount": 20
    };
    await requestGet("getFirstPage", formData: data).then((onValue) {
      CommunityFindModel findModel = CommunityFindModel.fromJson(onValue);
      setState(() {
        findList = findModel.data.dataList;
      });
    });
  }

  getTopicListPage(String lastId, String topicName) async {
    var data = {"lastId": lastId, "topicName": topicName, "showCount": 20};
    await requestGet("getTopicListPage", formData: data).then((onValue) {
      TopicModel topicModel = TopicModel.fromJson(onValue);
      setState(() {
        topicList = topicModel.data.topicInfoVOs;
      });
    });
  }
}
